package com.xuan.common.file.config;


import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;


import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.common.auth.DefaultCredentialProvider;
import jakarta.annotation.PreDestroy;
import jakarta.annotation.Resource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class OSSConfig {

    @Resource
    private OSSProperties properties;

    private OSS ossClient;

    @Bean
    public OSS ossClient() {
        DefaultCredentialProvider defaultCredentialProvider = CredentialsProviderFactory.
                newDefaultCredentialProvider(properties.getAccessKeyId(), properties.getSecretAccessKey());
        ossClient =  OSSClientBuilder.create()
                .endpoint(properties.getEndpoint())
                .credentialsProvider(defaultCredentialProvider)
                .region(properties.getRegion())
                .build();
        return ossClient;
    }


    @PreDestroy
    public void destroy() {
        ossClient.shutdown();
    }
}
